pub mod models;
pub mod repository;
pub mod migrations;

use sqlx::SqlitePool;
use anyhow::Result;

pub struct Database {
    pool: SqlitePool,
}

impl Database {
    pub async fn new(database_path: &str) -> Result<Self> {
        // 确保数据库目录存在
        if let Some(parent) = std::path::Path::new(database_path).parent() {
            std::fs::create_dir_all(parent)?;
        }
        
        let database_url = format!("sqlite:{}", database_path);
        let pool = SqlitePool::connect(&database_url).await?;
        
        Ok(Self { pool })
    }
    
    pub async fn run_migrations(&self) -> Result<()> {
        migrations::run_migrations(&self.pool).await?;
        Ok(())
    }
    
    pub fn pool(&self) -> &SqlitePool {
        &self.pool
    }
}
